.text
.section .rodata
.LC0:
    .string "Please enter the book title."
.LC1:
    .string "Now enter the author."
.LC2:
    .string "Now enter the value."
.LC3:
    .string "%f"
.LC4:
    .string "%s by %s: $%.2f\n"
.LC5:
    .string "%s: \"%s\" ($%.2f)\n"
.LC6:
    .string "Done."

.text
.globl main
.type main, @function
main:
    addi sp, sp, -96
    sd ra, 88(sp)
    sd s0, 80(sp)

    addi s0, sp, 80
    lui a5, %hi(.LC0)
    addi a0, a5, %lo(.LC0)
    call puts

    addi a0, s0, -80
    li a1, 41
    call s_gets

    lui a5, %hi(.LC1)
    addi a0, a5, %lo(.LC1)
    call puts

    addi a0, s0, -39
    li a1, 31
    call s_gets

    lui a5, %hi(.LC2)
    addi a0, a5, %lo(.LC2)
    call puts

    lui a5, %hi(.LC3)
    addi a0, a5, %lo(.LC3)
    addi a1, s0, -8
    call scanf

    lui a5, %hi(.LC4)
    addi a0, a5, %lo(.LC4)
    addi a1, s0, -80
    addi a2, s0, -39
    flw fa5, -8(s0)
    fcvt.d.s fa5, fa5
    fmv.x.d a3, fa5
    call printf

    lui a5, %hi(.LC5)
    addi a0, a5, %lo(.LC5)
    addi a1, s0, -39
    addi a2, s0, -80
    flw fa5, -8(s0)
    fcvt.d.s fa5, fa5
    fmv.x.d a3, fa5
    call printf

    ld ra, 8(s0)
    ld s0, 0(s0)
    addi sp, sp, 96
    jr ra


.type s_gets, @function
s_gets:
    addi sp, sp, -48
    sd ra, 40(sp)
    sd s0, 32(sp)
    addi s0, sp, 32

    // 根据x86的经验，就不存参数了，按照通用的规矩是要存的
    // ret_val: -8(s0)
    // i: -12(s0)

    // 不行，不存参数是不行的，因为调用fgets后，返回值会把a0覆盖了。
    // st:-20(s0)
    // n: -24(s0)
    sd a0, -20(s0) /* st */
    sw a1, -24(s0) /* n */
    sw zero, -12(s0) /* i */

    // 以下两行实际不需要，不过协助这里，万一中间插了代码把a0和a1覆盖了呢
    ld a0, -20(s0) /* st */
    lw a1, -24(s0) /* n */
    lui a5, %hi(stdin)
    ld a2, %lo(stdin)(a5)
    call fgets

    sd a0, -8(s0) /* ret_val */
    beq a0, zero, .EXIT

    j .WHILE_15_COMPARE
.WHILE_15_ENTER:
    lw a5, -12(s0) /* i */
    addiw a5, a5, 1
    sw a5, -12(s0) /* i */
.WHILE_15_COMPARE:
    lw a5, -12(s0) /* i */
    sext.w a5, a5
    ld a4, -20(s0) /* st */
    add a5, a4, a5
    lbu a5, (a5)
    li a4, 10
    beq a5, a4, .IF_17
    bne a5, zero, .WHILE_15_ENTER
.IF_17:
    lw a5, -12(s0) /* i */
    sext.w a5, a5
    ld a4, -20(s0) /* st */
    add a5, a4, a5
    lbu a6, (a5)
    li a4, 10
    bne a6, a4, .WHILE_20_ENTER
    sb zero, (a5)
    j .EXIT
.WHILE_20_ENTER:
    call getchar
    li a5, 10
    bne a0, a5, .WHILE_20_ENTER
.EXIT:
    ld a0, -8(s0) /* ret_val */
    ld ra, 40(sp)
    ld s0, 32(sp)
    addi sp, sp, 48
    jr ra